package com.kmxd.ams.app.arrange.command;

import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.google.common.collect.ImmutableSet;
import com.kmxd.ams.client.archive.IArchiveFieldSvc;
import com.kmxd.ams.client.archive.dto.ArchiveFieldQueryDTO;
import com.kmxd.ams.client.archive.vo.ArchiveFieldListVO;
import com.kmxd.ams.core.common.enums.ArchStatusEnum;
import com.kmxd.ams.core.common.enums.ParamsValidateType;
import com.kmxd.ams.core.exception.ParamsValidateException;
import com.kmxd.ams.infra.arrange.base.SystemBO;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * 数据未空校验
 *
 * @author nickbi
 */
@Component
@RequiredArgsConstructor
public class DataEmptyCheckCmd {
  private final IArchiveFieldSvc archiveFieldSvc;

  /**
   * 数据为空校验
   *
   * @param req
   * @param <T>
   */
  public <T extends SystemBO> void checkDataEmpty(T req) {
    if (!ImmutableSet.of(ArchStatusEnum.S0.getCode(), ArchStatusEnum.S1.getCode())
        .contains(req.getStatus())) {
      return;
    }
    ArchiveFieldQueryDTO qry = new ArchiveFieldQueryDTO();
    qry.setTableId(req.getTableId());
    if (ArchStatusEnum.S0.getCode().equals(req.getStatus())) {
      qry.setAttrEmptyWarn(Boolean.TRUE);
    }
    if (ArchStatusEnum.S1.getCode().equals(req.getStatus())) {
      qry.setAttrEmptyWarnArrange(Boolean.TRUE);
    }
    List<ArchiveFieldListVO> fieldList = archiveFieldSvc.selectList(qry);
    if (ObjectUtil.isEmpty(fieldList)) {
      return;
    }
    JSONObject parseObj = JSONUtil.parseObj(req);
    fieldList.forEach(
        item -> {
          String field = StringUtils.underlineToCamel(item.getDbFieldKey());
          String checkValue = parseObj.getStr(field);
          if (CharSequenceUtil.isBlank(checkValue)) {
            throw ParamsValidateException.instance(field, ParamsValidateType.NOT_NULL);
          }
        });
  }

  /**
   * 数据列表，为空校验
   *
   * @param req
   * @param fieldList
   * @param <T>
   */
  public <T extends SystemBO> void checkDataListEmpty(T req, List<ArchiveFieldListVO> fieldList) {
    if (!ImmutableSet.of(ArchStatusEnum.S0.getCode(), ArchStatusEnum.S1.getCode())
        .contains(req.getStatus())) {
      return;
    }
    if (ObjectUtil.isEmpty(fieldList)) {
      return;
    }
    JSONObject parseObj = JSONUtil.parseObj(req);
    fieldList.forEach(
        item -> {
          String field = StringUtils.underlineToCamel(item.getDbFieldKey());
          String checkValue = parseObj.getStr(field);
          if (CharSequenceUtil.isBlank(checkValue)) {
            throw ParamsValidateException.instance(field, ParamsValidateType.NOT_NULL);
          }
        });
  }

  /**
   * 数据list，校验数据空值
   *
   * @param list 数据列表
   * @param fieldList 字段列表
   * @param <T>
   */
  public <T extends SystemBO> void checkDataListEmpty(
      List<T> list, List<ArchiveFieldListVO> fieldList) {
    if (ObjectUtil.isEmpty(list)) {
      return;
    }
    if (ObjectUtil.isEmpty(fieldList)) {
      return;
    }

    list.forEach(
        data -> {
          if (!ImmutableSet.of(ArchStatusEnum.S0.getCode(), ArchStatusEnum.S1.getCode())
              .contains(data.getStatus())) {
            return;
          }
          JSONObject parseObj = JSONUtil.parseObj(data);
          fieldList.forEach(
              item -> {
                String field = StringUtils.underlineToCamel(item.getDbFieldKey());
                String checkValue = parseObj.getStr(field);
                if (CharSequenceUtil.isBlank(checkValue)) {
                  throw ParamsValidateException.instance(field, ParamsValidateType.NOT_NULL);
                }
              });
        });
  }
}
